function Axios(config) {
  this.config = config
  this.interceptors = {
    request: new InterceptorManager(),
    response: new InterceptorManager()
  }
}
Axios.prototype.request = function (config) {
  let promise = Promise.resolve(config)
  let charn = [dispatchRequest, undefined] //undefined是占位符
  this.interceptors.request.handlers.forEach(item => {
    charn.unshift(item.resove, item.reject)
  })
  this.interceptors.response.handlers.forEach(item => {
    charn.push(item.resove, item.reject)
  })
  while (charn.length) {
    promise = promise.then(charn.shift(), charn.shift())
  }

  return promise
}
function dispatchRequest(config) {
  return xhrAdapter(config)
}
function xhrAdapter(config) {
  return new Promise((resolve) => {
    resolve({
      status: 200,
      statusText: "ok"
    })
  })
}
function InterceptorManager() {
  this.handlers = []
}
InterceptorManager.prototype.use = function (resove, reject) {
  this.handlers.push({
    resove,
    reject
  })
}
const context = new Axios({})
let axios = Axios.prototype.request.bind(context)
Object.keys(context).forEach(key => {
  axios[key] = context[key]
})

axios.interceptors.request.use(config => {
  console.log("请求拦截器成功1号");
  return config
}, err => {
  console.log("请求拦截器失败1号");
  return Promise.reject(err)
})
axios.interceptors.request.use(config => {
  console.log("请求拦截器成功2号");
  return config
}, err => {
  console.log("请求拦截器失败2号");
  return Promise.reject(err)
})
axios.interceptors.response.use(config => {
  console.log("响应拦截器成功1号");
  return config
}, err => {
  console.log("响应拦截器失败1号");
  return Promise.reject(err)
})
axios.interceptors.response.use(config => {
  console.log("响应拦截器成功2号");
  return config
}, err => {
  console.log("响应拦截器失败2号");
  return Promise.reject(err)
})

axios({
  method: "get",
  url: 'http://localhost:3000/posts',
}).then(res => {
  console.log(res, 300
  );
}, err => {
  console.log(err);
})
